(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('waf.passwordController', passwordController);
	passwordController.$inject = [ '$rootScope', '$scope', '$q', '$locale', '$location', 'Constants', 'storageService', 'layoutConfig',
			'authService', 'validatorConfig', 'modalService', '$ocLazyLoad', 'ajaxServiceFactory', 'SYS_PARAMS' ];
	function passwordController($rootScope, $scope, $q, $locale, $location, Constants, storageService, layoutConfig, 
			authService, validatorConfig, modalService, $ocLazyLoad, ajaxServiceFactory, SYS_PARAMS) {
		var ctrl = this, userInfo = $scope.$stateParams.params, log = $scope.loggerFactory.getLogger("passwordController");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			var initPassword = userInfo.password;
			ctrl.user = userInfo;
			ctrl.user.initPassword = initPassword;
			ctrl.user.password = '';
		};

		ctrl.submit = function() {
			if (!ctrl.user.password) {
				modalService.openErrorDialogs({
					messages : [ 'js.pub.error.old-password-required' ]
				});
				return;
			}
			if (!ctrl.user.newPassword) {
				modalService.openErrorDialogs({
					messages : [ 'js.pub.error.new-password-required' ]
				});
				return;
			}
			if (!ctrl.user.confirmNewPassword) {
				modalService.openErrorDialogs({
					messages : [ 'js.pub.error.confirm-new-password-required' ]
				});
				return;
			}
			if (ctrl.user.initPassword != ctrl.user.password) {
				modalService.openErrorDialogs({
					messages : [ 'js.pub.error.old-password-wrong' ]
				});
				return;
			}
			if (ctrl.user.newPassword == ctrl.user.password) {
				modalService.openErrorDialogs({
					messages : [ 'js.pub.error.new-password-different' ]
				});
				return;
			}
			if (ctrl.user.newPassword != ctrl.user.confirmNewPassword) {
				modalService.openErrorDialogs({
					messages : [ 'js.pub.error.confirm-new-password-same' ]
				});
				return;
			}
			
			
			
			authService.mustChangePassword(ctrl.user).then(function(response) {
				modalService.openInfoDialogs({
					'messages' : [ 'title.dialog.changePassword' ],
					'btnCallback' : {
						'close' : function() {
							$scope.go('signon');
						}
					}
				});
			},function(response) {
				modalService.openErrorDialogs({
					'messages' : response.error
				});
				log.debug("change password error:" + angular.toJson(response.error));
			});
		};
		
	}
})(angular);